package com.tuling;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.*;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class UrlPool {

    public static void main(String[] args) {
        getUrl("https://nipic.com/");
    }

    public static void getUrl(String baseUrL){
        LinkedHashMap<String, Boolean> oldMap = new LinkedHashMap<>();
        String oldLink = "";
        Pattern pattern = Pattern.compile("https://.*");
        Matcher matcher = pattern.matcher(baseUrL);
        if(matcher.find()){
            oldLink = matcher.group();
        }
        oldMap.put(baseUrL,false);

        crawpScript(oldLink,oldMap);

        for(Map.Entry<String,Boolean> entry:oldMap.entrySet()){
            System.out.println("匹配到的连接："+entry.getKey());
        }

    }

    private static void crawpScript(String oldLink, LinkedHashMap<String, Boolean> oldMap) {
        LinkedHashMap<String, Boolean> newMap = new LinkedHashMap<String, Boolean> ();

        for(Map.Entry<String, Boolean>  mapping:oldMap.entrySet()){
            if(mapping.getValue()){
                oldLink = mapping.getKey();
                try {
                    URL url = new URL(oldLink);
                    HttpURLConnection urlConnection =(HttpURLConnection) url.openConnection();
                    urlConnection.setRequestMethod("GET");
                    if(urlConnection.getResponseCode() == 200){
                        BufferedReader reader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
                        reader.read();
                        String regex = "<a Class= [a-z]+href=\"(.*?)\"≥(.*?)</a>";
                        Pattern pattern = Pattern.compile(regex);
                        Matcher matcher = null;
                        String line = "";
                        while ((line = reader.readLine()) != null){
                            matcher = pattern.matcher(line);
                            if(matcher.find()){

                            }

                        }
                    }
                } catch (Exception e) {
                    System.out.println(e.getMessage());
                }
            }
        }

    }
}
